package com.zsrt.day20170425.pm.xmlcrud.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXWriter;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

import com.zsrt.day20170411.am.Student;
import com.zsrt.day20170425.pm.xmlcrud.dao.StudentCRUDDao;

/**
*@author jzh
*@创建时间 2017年4月25日下午2:29:44
*类说明：
*/
public class StudentCRUDDaoImpl implements StudentCRUDDao{

	
	
	@Override
	public void addStudent(File file) throws Exception {
		SAXReader saxReader=new SAXReader();
		Document document = saxReader.read(file);
		Element root = document.getRootElement();
		Element resource=root.addElement("student");
	    Element name=resource.addElement("name");
	    Element sex=resource.addElement("sex");
	    Element age=resource.addElement("age");
	    name.setText("王震");
	    sex.setText("女");
	    age.setText("19");
	    writer(document);
	}

	/*@Override
	public Element getRoot(File file) throws Exception {
		SAXReader saxReader=new SAXReader();
		Document document = saxReader.read(file);
		Element root = document.getRootElement();
		return root;*/
		/*Element element = root.element("student");
		Element name = element.element("name");
		name.setText("王五");
		String text = name.getText();
		System.out.println(text);
		writer(document);
		SAXWriter saxWriter=new SAXWriter();
		saxWriter.write(document);*/
		
	/*}*/
	
	 public void writer(Document document) throws Exception {  
	        // 紧凑的格式  
	        // OutputFormat format = OutputFormat.createCompactFormat();  
	        // 排版缩进的格式  
	        OutputFormat format = OutputFormat.createPrettyPrint();  
	        // 设置编码  
	        format.setEncoding("UTF-8");  
	        // 创建XMLWriter对象,指定了写出文件及编码格式  
	        // XMLWriter writer = new XMLWriter(new FileWriter(new  
	        // File("src//a.xml")),format);  
	        XMLWriter writer = new XMLWriter(new OutputStreamWriter(  
	                new FileOutputStream(new File("iotest/test1.xml")), "UTF-8"), format);  
	        // 写入  
	        writer.write(document);  
	        // 立即写入  
	        writer.flush();  
	        // 关闭操作  
	        writer.close();  
	    }

	@Override
	public void deleteStudent(File file) throws Exception {
		SAXReader saxReader=new SAXReader();
		Document document = saxReader.read(file);
		Element root = document.getRootElement();
		Element element = root.element("student");
		root.remove(element);
		writer(document);
		
	}
	
	public void deleteAttribute(File file) throws Exception{
		SAXReader saxReader=new SAXReader();
		Document document = saxReader.read(file);
		Element root = document.getRootElement();
		Element student1 = root.element("student1");
		//Attribute name = student1.attribute("name");
		Element name = student1.element("name");
		boolean flag = student1.remove(name);
		if(flag){
			System.out.println("删除成功");
		}else{
			System.out.println("删除失败");
		}
		writer(document);
		
		
	}

	@Override
	public void findAllElements(File file) throws Exception {
		SAXReader saxReader=new SAXReader();
		Document document = saxReader.read(file);
		Element root = document.getRootElement();
		getNodes(root);
		
	}  
	
	public void getNodes(Element node){  
	    //当前节点的名称、文本内容和属性  
	    System.out.println("当前节点名称："+node.getName());//当前节点名称  
	    System.out.println("当前节点的内容："+node.getTextTrim());//当前节点名称  
	    List<Attribute> listAttr=node.attributes();//当前节点的所有属性的list  
	    for(Attribute attr:listAttr){//遍历当前节点的所有属性  
	        String name=attr.getName();//属性名称  
	        String value=attr.getValue();//属性的值  
	        System.out.println("属性名称："+name+"属性值："+value);  
	    }  
	      
	    //递归遍历当前节点所有的子节点  
	    List<Element> listElement=node.elements();//所有一级子节点的list  
	    for(Element e:listElement){//遍历所有一级子节点  
	        this.getNodes(e);//递归  
	    }  
	}

	@Override
	public void updateAttribute(File file) throws Exception {
		SAXReader saxReader=new SAXReader();
		Document document = saxReader.read(file);
		Element root = document.getRootElement();
		Element student1 = root.element("student1");
		Element name = student1.element("name");
		getElement(student1);
		name.setText("赵四");
		writer(document);
		
	}  
	
	
	public void WriteToXML(Document document) throws Exception{
		OutputFormat format=OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		XMLWriter writer=new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File("iotest/test1.xml")),"UTF-8"),format);
		writer.write(document);
		writer.flush();
		writer.close();
	}
	
	public void getElement(Element node){
		
		System.out.println("当前节点的名字是："+node.getName());
		System.out.println("当前节点的属性是："+node.getTextTrim());
		List<Attribute> atrLists = node.attributes();
		for(Attribute atrList:atrLists){
			String name = atrList.getName();
			String value = atrList.getValue();
			System.out.println("属性名是："+name+"属性值是："+value);
		}
		List<Element> eleLists = node.elements();
		for(Element eleList:eleLists){
			getElement(eleList);
		}
		
	}

}
